package com.example.addressbook.views;

import java.util.Collection;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.swt.custom.CBanner;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;

import com.example.addressbook.entities.Address;
import com.example.addressbook.entities.Country;
import com.example.addressbook.services.AddressBookServices;
import com.example.addressbook.services.IAddressService;

public class AddressView extends ViewPart {

	private Text tfName;
	private Text tfStreet;
	private Text tfZip;
	private Text tfCity;
	private Label lblName;
	private Combo cbCountry;
	private Collection<Country> countries;
	private ComboViewer comboViewer;
	
	public AddressView() {
	}

	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(3, false));
		{
			lblName = new Label(container, SWT.NONE);
			lblName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblName.setAlignment(SWT.RIGHT);
			lblName.setText("Name:");
		}
		{
			tfName = new Text(container, SWT.BORDER);
			tfName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		}
		{
			Label lblStrae = new Label(container, SWT.NONE);
			lblStrae.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblStrae.setAlignment(SWT.RIGHT);
			lblStrae.setText("Stra\u00DFe:");
		}
		{
			tfStreet = new Text(container, SWT.BORDER);
			tfStreet.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		}
		{
			Label lblPlzOrt = new Label(container, SWT.NONE);
			lblPlzOrt.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblPlzOrt.setAlignment(SWT.RIGHT);
			lblPlzOrt.setText("PLZ Ort:");
		}
		{
			tfZip = new Text(container, SWT.BORDER);
			GridData gd_tfZip = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
			gd_tfZip.widthHint = 50;
			tfZip.setLayoutData(gd_tfZip);
		}
		{
			tfCity = new Text(container, SWT.BORDER);
			tfCity.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		}
		{
			Label lblLand = new Label(container, SWT.NONE);
			lblLand.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblLand.setAlignment(SWT.RIGHT);
			lblLand.setText("Land:");
		}
		{
			comboViewer = new ComboViewer(container, SWT.READ_ONLY);
			cbCountry = comboViewer.getCombo();
			cbCountry.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
			comboViewer.setContentProvider( ArrayContentProvider.getInstance());
			comboViewer.setLabelProvider( new LabelProvider(){
				@Override
				public String getText(Object element) {
					Country country = (Country) element;
					return country.getName();
				}
			});
			comboViewer.addSelectionChangedListener( new ISelectionChangedListener() {
				@Override
				public void selectionChanged(SelectionChangedEvent event) {
					IStructuredSelection selection = (IStructuredSelection) event.getSelection();
					Country country = (Country) selection.getFirstElement();
					System.out.println("Country: " + country.getName());
				}
			});
			loadCountries();
		}

		createActions();
		initializeToolBar();
		initializeMenu();
		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener( new ISelectionListener() {
			@Override
			public void selectionChanged(IWorkbenchPart part, ISelection selection) {
				if (selection != null && selection instanceof IStructuredSelection) {
					IStructuredSelection element = (IStructuredSelection) selection;
					Address address = (Address) element.getFirstElement();
					if (address != null)
						setAddress( address);
				}
			}
		});
	}

	private void setAddress(Address address) {
		tfName.setText( address.getName());
		tfStreet.setText( address.getStreet());
		tfZip.setText(address.getZip());
		tfCity.setText(address.getCity());
		comboViewer.setSelection( new StructuredSelection(address.getCountry()));
	}
	
	private void loadCountries() {
		Job job = new Job("Load Countries") {
			
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				IAddressService service = AddressBookServices.getAddressService();
				countries = service.getAllCountries();
				displayCountries();
				return Status.OK_STATUS;
			}
		};
		job.schedule();
	}

	private void displayCountries() {
		UIJob uijob = new UIJob("Display Countries") {
			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				comboViewer.setInput( countries);
				return Status.OK_STATUS;
			}
		};
		uijob.schedule();
	}
	
	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		tfName.setFocus();
	}

}
